package com.songaw.cangku.service;

import com.songaw.cangku.mapper.AppAccountExMapper;
import com.songaw.cangku.pojo.domain.AppAccountVo;
import com.songaw.cangku.pojo.domain.AppLoginVo;
import com.songaw.cangku.pojo.entity.AppAccount;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhaoxinguo on 2017/9/13.
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    private AppAccountExMapper appAccountExMapper;

    /**
     * 通过构造器注入UserRepository
     * @param appAccountExMapper
     */
    public UserDetailsServiceImpl(AppAccountExMapper appAccountExMapper) {
        this.appAccountExMapper = appAccountExMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        AppAccountVo search = new AppAccountVo();
        search.setEmplno(username);
        List<AppAccount> list = appAccountExMapper.select(search);
        if(list == null||list.size()==0){
            throw new UsernameNotFoundException(username);
        }
        AppAccount appAccount = list.get(0);
        AppLoginVo appLoginAccountVo = new AppLoginVo();
        BeanUtils.copyProperties(appAccount,appLoginAccountVo);

        return appLoginAccountVo;//new org.springframework.security.core.userdetails.User(appAccount.getEmplno(), appAccount.getPassword(), emptyList());
    }

}
